/*
*
*	Purpose: to provide wrapper functions useful in Lua
*
*/

#pragma once
#include "Lua.h"

/* Comment these lines out to disable visual or execution log debugging */


#ifdef LUA_CONSOLE_DEBUG
#include <iostream>
using namespace std;
#endif

#ifdef LUA_EXECUTE_DEBUG
#include "ExecutionLog.h"
#endif

// Where we get our functions from
#include "GUIManager.h"

// Use a namespace to keep the global namespace "clean"
namespace LUA_UI_FUNCTIONS
{
	// An error handling function, for simplicity (checks the number of arguments a function has recieved and gives a warning
	bool CheckArgumentCount(int total_arguments, const char* function_name)
	{
		if(Lua->GetParamaterCount() != total_arguments)
		{
			#ifdef LUA_CONSOLE_DEBUG
			cout << "\nLua Warning - Bad param count passed to " << function_name << "; expected " << total_arguments << endl;
			#endif
			#ifdef LUA_EXECUTE_DEBUG
			ExecuteLog->AddMessage("Lua Warning - Bad param count passed to a function");
			#endif
			return false;
		}

		//Log->AddMessage("Lua function[ " + GetString(function_name) + " ] called.");

		return true;
	}

	// Our wrapper functions
	// Used to display a message from Lua
	LuaGlue l_GUI_CreatePanel (lua_State *L)
	{
		// Error handling
		CheckArgumentCount(0, "GUI_CreatePanel");

			
		// Create the panel
		GUI->CreatePanel();

		return 0; // 0 results, needed by lua
	}

	LuaGlue l_GUI_SetLocation(lua_State *L)
	{
		// Error handling
		CheckArgumentCount(4, "GUI_SetLocation");
		
		// Function call
		GUI->SetLocation((int)Lua->GetNumberArgument(1, 0.0), (int)Lua->GetNumberArgument(2, 0.0),
			(int)Lua->GetNumberArgument(3, 0.0), (int)Lua->GetNumberArgument(4, 0.0));


		return 0;
	}

	LuaGlue l_GUI_SetColor(lua_State *L)
	{
		CheckArgumentCount(1, "GUI_SetColor");

		GUI->SetColor(Key->GetColor(Lua->GetStringArgument(1, "black")));

		return 0;

	}

	LuaGlue l_GUI_SetFadeTime(lua_State *L)
	{

		CheckArgumentCount(1, "GUI_SetFadeTime");

		GUI->SetFadeTime(float(Lua->GetNumberArgument(1, 3.0)));

		return 0;
	}

	LuaGlue l_GUI_AddButton(lua_State *L)
	{
		if(Lua->GetParamaterCount() == 2)
			GUI->AddButton((int)Lua->GetNumberArgument(1, 0.0), Lua->GetStringArgument(2, "ignored_tile"));
		else if(Lua->GetParamaterCount() == 3)
			GUI->AddButton((int)Lua->GetNumberArgument(1, 0.0), Lua->GetStringArgument(2, "ignored_tile"), Lua->GetStringArgument(3, "ignored_tile"));
		else if(Lua->GetParamaterCount() == 6)
			GUI->AddButton((int)Lua->GetNumberArgument(1, 0.0), Lua->GetStringArgument(2, "ignored_tile"),
				(int)Lua->GetNumberArgument(3, 0.0), (int)Lua->GetNumberArgument(4, 0.0), (int)Lua->GetNumberArgument(5, 0.0),
				(int)Lua->GetNumberArgument(6, 0.0));
		else if(Lua->GetParamaterCount() == 7)
			GUI->AddButton(Lua->GetNumberArgument(1, 0.0), Lua->GetStringArgument(2, "ignored_tile"),  Lua->GetStringArgument(3, "ignored_tile"),
				Lua->GetNumberArgument(4, 0.), Lua->GetNumberArgument(5, 0.), Lua->GetNumberArgument(6, 0.), Lua->GetNumberArgument(7, 0.));
		else
			CheckArgumentCount(2, "GUI_AddButton");

		return 0;
	}

	LuaGlue l_GUI_SetButtonDimensions(lua_State *L)
	{
		CheckArgumentCount(2, "GUI_SetButtonDimensions");

		GUI->SetButtonDimensions((int)Lua->GetNumberArgument(1, 0.0), (int)Lua->GetNumberArgument(2, 0.0));

		return 0;
	}

	LuaGlue l_GUI_ClearButtons(lua_State *L)
	{
		CheckArgumentCount(0, "GUI_ClearButtons");

		GUI->ClearButtons();

		return 0;
	}

	LuaGlue l_GUI_RegisterPanel(lua_State *L)
	{
		CheckArgumentCount(1, "GUI_RegisterPanel");

		GUI->RegisterPanel((int)Lua->GetNumberArgument(1, 0.0));

		return 0;
	}

	LuaGlue l_GUI_ActivatePanel(lua_State *L)
	{
		CheckArgumentCount(0, "GUI_ActivatePanel");

		GUI->ActivatePanel();

		return 0;
	}

	LuaGlue l_GUI_DeactivatePanel(lua_State *L)
	{
		CheckArgumentCount(0, "GUI_DeactivatePanel");

		GUI->DeactivePanel();

		return 0;
	}

	LuaGlue l_GUI_AddMiniPanel(lua_State *L)
	{
		CheckArgumentCount(5, "GUI_AddMiniPanel");

		GUI->AddMiniPanel((int)Lua->GetNumberArgument(1, 0.0), (int)Lua->GetNumberArgument(2, 0.0), (int)Lua->GetNumberArgument(3, 0.0),
			(int)Lua->GetNumberArgument(4, 0.0), Key->GetColor(Lua->GetStringArgument(5, "")));

		return 0;
	}

	LuaGlue l_GUI_AddText(lua_State *L)
	{
		if(Lua->GetParamaterCount() == 5)
		{
			CheckArgumentCount(5, "GUI_AddText");
			GUI->AddText((int)Lua->GetNumberArgument(1, 0.0), (int)Lua->GetNumberArgument(2, 0.0),
				Lua->GetStringArgument(3, ""), Key->GetColor(Lua->GetStringArgument(4, "")), Lua->GetStringArgument(5, ""));

		}
		else if(CheckArgumentCount(3, "GUI_AddText"))
			GUI->AddText(Lua->GetStringArgument(1, ""), Key->GetColor(Lua->GetStringArgument(2, "")), Lua->GetStringArgument(3, ""));


		return 0;
	}

	LuaGlue l_GUI_SetRelButDist(lua_State *L)
	{
		CheckArgumentCount(2, "GUI_SetRelButDist");

		GUI->SetRelButDist((int)Lua->GetNumberArgument(1, 0.0), (int)Lua->GetNumberArgument(2, 0.0));

		return 0;
	}
}

// Where are actually add functions
void InitLuaUIFunctions()
{
	using namespace LUA_UI_FUNCTIONS;

	// first parameter is what the function name will be in lua and the second paramter is the actual function in C++
	Lua->AddFunction("GUI_CreatePanel", l_GUI_CreatePanel);
	Lua->AddFunction("GUI_SetLocation", l_GUI_SetLocation);
	Lua->AddFunction("GUI_SetColor", l_GUI_SetColor);
	Lua->AddFunction("GUI_SetFadeTime", l_GUI_SetFadeTime);
	Lua->AddFunction("GUI_AddButton", l_GUI_AddButton);
	Lua->AddFunction("GUI_SetButtonDimensions", l_GUI_SetButtonDimensions);
	Lua->AddFunction("GUI_ClearButtons", l_GUI_ClearButtons);
	Lua->AddFunction("GUI_RegisterPanel", l_GUI_RegisterPanel);
	Lua->AddFunction("GUI_ActivatePanel", l_GUI_ActivatePanel);
	Lua->AddFunction("GUI_DeactivatePanel", l_GUI_DeactivatePanel);
	Lua->AddFunction("GUI_AddMiniPanel", l_GUI_AddMiniPanel);
	Lua->AddFunction("GUI_AddText", l_GUI_AddText);
	Lua->AddFunction("GUI_SetRelButDist", l_GUI_SetRelButDist);

}